package cn.tedu.mall.producer.controller;


import cn.tedu.mall.producer.pojo.dto.BrandAddNewDTO;
import cn.tedu.mall.producer.pojo.dto.BrandEditDTO;
import cn.tedu.mall.producer.pojo.vo.BrandListItemVO;
import cn.tedu.mall.producer.service.IBrandService;
import cn.tedu.mall.producer.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 品牌控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Api(tags = "1. 品牌管理模块")
@RestController
@RequestMapping("/brands")
public class BrandController {

    @Autowired
    private IBrandService brandService;

    public BrandController() {
        log.debug("创建控制器对象：BrandController");
    }

    // http://localhost:9080/brands/add-new
    @ApiOperation("添加品牌")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody @Valid BrandAddNewDTO brandAddNewDTO) {
        log.debug("接收到的请求参数：{}", brandAddNewDTO);
        brandService.addNew(brandAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/brands/12/delete
    @ApiOperation("删除品牌")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable Long id) {
        brandService.deleteById(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/brands/6937/edit?name=XiaoMi&pinyin=xiaomi&logo=xxx&categoryId=998&description=hahaha&keywords=mi&sort=66
    @ApiOperation("编辑品牌")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/edit")
    public String edit(@PathVariable Long id, BrandEditDTO brandEditDTO) {
        log.debug("接收到的请求参数：id=" + id);
        log.debug("接收到的请求参数：" + brandEditDTO);
        return "尝试编辑品牌（尚未完成）";
    }

    // http://localhost:9080/brands
    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult list() {
        log.debug("接收到查询品牌列表的请求");
        List<BrandListItemVO> brands = brandService.list();
        return JsonResult.ok(brands);
    }

}
